import { _decorator, Button, Label, Sprite } from 'cc';
import { UIComponent } from '../../../Framework/Scripts/Managers/UIComponent';
import { AudioCfg, AudioMgr } from '../../../Framework/Scripts/Managers/AudioMgr';
import { CocosUtils } from '../../../Framework/Scripts/Utils/CocosUtils';
import { UIMgr } from '../../../Framework/Scripts/Managers/UIMgr';
import { NativeMgr } from '../../../Framework/Scripts/Engine/NativeMgr';
import { AbNames } from '../Config/ResConfig';
const { ccclass, property } = _decorator;

@ccclass('GameSet_Ctr')
export class GameSet_Ctr extends UIComponent {
    spEffectLogo: Sprite;
    spEffect: Sprite;
    labFz: Label;
    spMusicLogo: Sprite;
    spMusic: Sprite;
    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
    }

    protected onDestroy(): void {
    }

    private InitUI(){
        let root = this.ViewNode('root');
        this.AddOpenAni(root);

        this.spEffectLogo = this.ViewComponent('root/spEffect',Sprite);
        this.spEffect = this.ViewComponent('root/spBtnEffect',Sprite);
        this.spMusicLogo = this.ViewComponent('root/spMusic',Sprite);
        this.spMusic = this.ViewComponent('root/spBtnMusic',Sprite);

        this.labFz = this.ViewComponent('root/labKeFu',Label);

        this.AddButtonListener('mask',this,this.OnBtnCloseClick);
        this.AddButtonListener('root/spBtnEffect',this,this.onBtnEffectClick);
        this.AddButtonListener('root/spBtnMusic',this,this.onBtnMusicClick);
        this.AddButtonListener('root/labFuZi',this,this.onBtnFZClick )
    }

    private InitData(){
        let isOpenMusic = CocosUtils.localStorageGet(AudioCfg.sound_musicKey);
        if(isOpenMusic === undefined || isOpenMusic === null){
            CocosUtils.localStorageSet(AudioCfg.sound_effectKey,1);
            this.UpdateMusic(true);
        }else{
            isOpenMusic = parseInt(isOpenMusic);
            if(isOpenMusic == 1){
                this.UpdateMusic(true);
            }else{
                this.UpdateMusic(false);
            }
        }

        let isOpenEffect = CocosUtils.localStorageGet(AudioCfg.sound_effectKey);
        if(isOpenEffect === undefined || isOpenEffect === null){
            CocosUtils.localStorageSet(AudioCfg.sound_effectKey,1);
            this.UpdateEffect(true);
        }else{
            isOpenEffect = parseInt(isOpenEffect);
            if(isOpenEffect == 1){
                this.UpdateEffect(true);
            }else{
                this.UpdateEffect(false);
            }
        }
    }

    private UpdateEffect(isOpen: boolean){
        let atals = UIMgr.inst.getAtals('Atals/GameHall','GameHall');
        if(atals){
            if(isOpen){
                this.spEffectLogo.spriteFrame =  atals.getSpriteFrame('GameSet_icon_on');
                this.spEffect.spriteFrame = atals.getSpriteFrame('GameSet_on');
            }else{
                this.spEffectLogo.spriteFrame =  atals.getSpriteFrame('GameSet_icon_off');
                this.spEffect.spriteFrame = atals.getSpriteFrame('GameSet_off');
            }
        }
    }

    private onBtnEffectClick(btn:Button){
        let isOpen = CocosUtils.localStorageGet(AudioCfg.sound_effectKey);
        if(isOpen){
            isOpen = parseInt(isOpen);
            if(isOpen == 1){
                CocosUtils.localStorageSet(AudioCfg.sound_effectKey,0);
                this.UpdateEffect(false);
                AudioMgr.inst.stopOneShot(true);
            }else if(isOpen == 0){
                CocosUtils.localStorageSet(AudioCfg.sound_effectKey,1);
                this.UpdateEffect(true);
                AudioMgr.inst.stopOneShot(true);
            }
        }
    }

    private UpdateMusic(isOpen: boolean){
        let atals = UIMgr.inst.getAtals('Atals/GameHall','GameHall');
        if(atals){
            if(isOpen){
                this.spMusicLogo.spriteFrame =  atals.getSpriteFrame('GameSet_icon1_on');
                this.spMusic.spriteFrame = atals.getSpriteFrame('GameSet_on');
            }else{
                this.spMusicLogo.spriteFrame =  atals.getSpriteFrame('GameSet_icon1_off');
                this.spMusic.spriteFrame = atals.getSpriteFrame('GameSet_off');
            }
        }
    }

    private onBtnMusicClick(btn:Button){
        let isOpen = CocosUtils.localStorageGet(AudioCfg.sound_musicKey);
        if(isOpen){
            isOpen = parseInt(isOpen);
            if(isOpen == 1){
                CocosUtils.localStorageSet(AudioCfg.sound_musicKey,0);
                this.UpdateMusic(false);
                AudioMgr.inst.pause();
            }else if(isOpen == 0){
                CocosUtils.localStorageSet(AudioCfg.sound_musicKey,1);
                this.UpdateMusic(true);
                if(AudioMgr.inst.isPlaying() == false){
                    AudioMgr.inst.play(AbNames.Common,'Sounds/bgm');
                    CocosUtils.localStorageSet(AudioCfg.sound_musicKey,1);
                }else{
                    AudioMgr.inst.resume();
                }
            }
        }
    }

    //复制
    private onBtnFZClick(btn:Button){
        NativeMgr.inst.capyToClipBoard(this.labFz.string)
    }

    update(deltaTime: number) {

    }
}


